package cn.iocoder.yudao.module.project.controller.admin.constructionunit;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.project.controller.admin.constructionunit.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.constructionunit.ConstructionUnitDO;
import cn.iocoder.yudao.module.project.service.constructionunit.ConstructionUnitService;

@Tag(name = "管理后台 - 施工单位")
@RestController
@RequestMapping("/project/construction-unit")
@Validated
public class ConstructionUnitController {

    @Resource
    private ConstructionUnitService constructionUnitService;

    @PostMapping("/create")
    @Operation(summary = "创建施工单位")
    @PreAuthorize("@ss.hasPermission('project:construction-unit:create')")
    public CommonResult<Long> createConstructionUnit(@Valid @RequestBody ConstructionUnitSaveReqVO createReqVO) {
        return success(constructionUnitService.createConstructionUnit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新施工单位")
    @PreAuthorize("@ss.hasPermission('project:construction-unit:update')")
    public CommonResult<Boolean> updateConstructionUnit(@Valid @RequestBody ConstructionUnitSaveReqVO updateReqVO) {
        constructionUnitService.updateConstructionUnit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除施工单位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('project:construction-unit:delete')")
    public CommonResult<Boolean> deleteConstructionUnit(@RequestParam("id") Long id) {
        constructionUnitService.deleteConstructionUnit(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除施工单位")
                @PreAuthorize("@ss.hasPermission('project:construction-unit:delete')")
    public CommonResult<Boolean> deleteConstructionUnitList(@RequestParam("ids") List<Long> ids) {
        constructionUnitService.deleteConstructionUnitListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得施工单位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('project:construction-unit:query')")
    public CommonResult<ConstructionUnitRespVO> getConstructionUnit(@RequestParam("id") Long id) {
        ConstructionUnitDO constructionUnit = constructionUnitService.getConstructionUnit(id);
        return success(BeanUtils.toBean(constructionUnit, ConstructionUnitRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "simple-list"})
    @Operation(summary = "获取施工单位全列表", description = "只包含被开启的施工单位，主要用于前端的下拉选项")
    public CommonResult<List<ConstructionUnitSimpleRespVO>> getSimplePostList() {
        // 获得施工单位列表，只要开启状态的
        List<ConstructionUnitDO> list = constructionUnitService.getConstructionUnitList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        // 排序后，返回给前端
        list.sort(Comparator.comparing(ConstructionUnitDO::getName));
        return success(BeanUtils.toBean(list, ConstructionUnitSimpleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得施工单位分页")
    @PreAuthorize("@ss.hasPermission('project:construction-unit:query')")
    public CommonResult<PageResult<ConstructionUnitRespVO>> getConstructionUnitPage(@Valid ConstructionUnitPageReqVO pageReqVO) {
        PageResult<ConstructionUnitDO> pageResult = constructionUnitService.getConstructionUnitPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConstructionUnitRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出施工单位 Excel")
    @PreAuthorize("@ss.hasPermission('project:construction-unit:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConstructionUnitExcel(@Valid ConstructionUnitPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConstructionUnitDO> list = constructionUnitService.getConstructionUnitPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "施工单位.xls", "数据", ConstructionUnitRespVO.class,
                        BeanUtils.toBean(list, ConstructionUnitRespVO.class));
    }

}